﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Xml.Serialization;
using ExChange.Model;
using System.Linq;
using System.Data.Entity;

namespace ExChange.Web.Controllers
{
    public class HomeController : BaseController
    {
        public JFDHEntities entity = new JFDHEntities();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {

            return View();
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        public ActionResult LoginOut()
        {
            LoginAdmin = null;
            return RedirectToAction("Login");
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="password"></param>
        /// <param name="account"></param>
        /// <returns></returns>

        public ActionResult UpdatePwd(string password, string account = "admin")
        {
            var admin = entity.Admin.FirstOrDefault(x => x.Account == account);
            if (admin != null)
            {
                admin.Password = password.ToMD532();
                entity.Entry<Admin>(admin).State = EntityState.Modified;
                int row = entity.SaveChanges();
                return LoginOut();
                //return row > 0 ? Json(new JsonData().Result(true), JsonRequestBehavior.AllowGet) : Json(new JsonData().Result(false));
            }
            else
            {
                return Json(new JsonData().Result(false));
            }
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public JsonResult LoginAjax()
        {
            string account = Request["account"];
            string password = Request["password"];
            json = new JsonData();
            var admin = entity.Admin.FirstOrDefault(x => x.Account == account);
            if (admin != null)
            {
                if (admin.Password.Equals(password.ToMD532()))
                {

                    LoginAdmin = admin;
                    json.Success();
                }
                else
                {
                    json.message = "密码错误！";
                }
            }
            else
            {
                json.message = "用户不存在！";
            }
            return Json(json, JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 统一上传
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadFile()
        {
            HttpFileCollectionBase files = Request.Files;
            var result = new Model.JsonData.Messager();
            if (files.Count > 0)
            {
                DateTime time = DateTime.Now;
                HttpPostedFileBase file = files[0];
                string file_path = Server.MapPath("/") + "Upload/" + time.Year + "/" + time.Month + "/" + time.Day + "/";
                if (!Directory.Exists(file_path))
                {
                    Directory.CreateDirectory(file_path);
                }
                string fileName = Guid.NewGuid().ToString().Replace("-", "");
                if (!string.IsNullOrEmpty(file.FileName))
                {
                    fileName += file.FileName.Substring(file.FileName.Length - 4);
                    var filetype = Path.GetExtension(file.FileName).ToLower();
                    if (filetype == ".png" || filetype == ".jpg" || filetype == ".gif" || filetype == ".mp4" || filetype == ".fly")
                    {
                        file.SaveAs(file_path + fileName);
                        result.data = new
                        {
                            path = "/" + "Upload/" + time.Year + "/" + time.Month + "/" + time.Day + "/" + fileName
                        };
                        result.success = true;
                        result.msg = "成功";

                    }
                    else
                    {
                        result.msg = "格式错误，仅限[.jpg|.png|.gif]";
                    }
                }
                else
                {
                    result.msg = "出错了";
                }

            }
            return Content(result.ToJsonString(), "text/html");
        }

    }


}
